package cn.regexp.demo.extension;

import cn.regexp.demo.action.CommentAction;
import com.intellij.diff.DiffContext;
import com.intellij.diff.FrameDiffTool;
import com.intellij.diff.requests.DiffRequest;
import com.intellij.diff.tools.simple.SimpleDiffTool;
import com.intellij.diff.tools.simple.SimpleDiffViewer;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.Separator;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author Regexpei
 * @date 2024/6/10 20:07
 * @description
 */
public class CommentDiffTool implements FrameDiffTool {
    @NotNull
    @Override
    public DiffViewer createComponent(@NotNull DiffContext diffContext, @NotNull DiffRequest diffRequest) {
        return new SimpleCommentDiffViewer(diffContext, diffRequest);
    }

    @Override
    public @NotNull @Nls(capitalization = Nls.Capitalization.Sentence) String getName() {
        return SimpleDiffTool.INSTANCE.getName();
    }

    @Override
    public boolean canShow(@NotNull DiffContext diffContext, @NotNull DiffRequest diffRequest) {
        return true;
    }


    private static class SimpleCommentDiffViewer extends SimpleDiffViewer {
        public SimpleCommentDiffViewer(@NotNull DiffContext context, @NotNull DiffRequest request) {
            super(context, request);
        }

        @Override
        protected @NotNull List<AnAction> createToolbarActions() {
            // 获取原本的 toolbar
            List<AnAction> toolbarActions = super.createToolbarActions();
            // 添加分隔符
            toolbarActions.add(Separator.getInstance());
            // 添加新按钮
            toolbarActions.add(new CommentAction());
            return toolbarActions;
        }
    }
}
